﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WuxiWTO.Mapping;
using WutiWTO.Common;
using System.Data.Linq;


namespace WuxiWTO.Web
{
    public partial class Register : System.Web.UI.Page
    {
        WTODataContext ctx = new WTODataContext();
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrEmpty(this.txtLoginNameP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "namef", "alert('用户名为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtPasswordP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "passf", "alert('密码为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtEmailP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "emailf", "alert('Email为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtNameP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "mingf", "alert('姓名为必填项');", true);
                return;
            }
            if (string.IsNullOrEmpty(this.txtIdentityP.Text.Trim()))
            {
                ClientScript.RegisterStartupScript(this.GetType(), "idf", "alert('身份证号为必填项');", true);
                return;
            }
            try
            {
                supporter sup = ctx.supporters.SingleOrDefault(p => p.supporter_login_name == this.txtLoginNameP.Text.Trim());
                if (sup != null)
                {
                    ClientScript.RegisterStartupScript(this.GetType(), "dupname", "alert('用户名已经存在，请更改');", true);
                    return;
                }
                sup = new supporter();
                sup.supporter_login_name = this.txtLoginNameP.Text.Trim();
                sup.supporter_password = this.txtPasswordP.Text.Trim();
                sup.email = this.txtEmailP.Text.Trim();
                sup.supporter_name = this.txtNameP.Text.Trim();
                sup.telephone = this.txtTelephoneP.Text.Trim();
                sup.identity_card = this.txtIdentityP.Text.Trim();
                sup.supporter_type = 'I';
                sup.is_active = 'N';
                sup.created_date = DateTime.Now;
                sup.update_date = DateTime.Now;
                ctx.supporters.InsertOnSubmit(sup);
                ctx.SubmitChanges();
                ClientScript.RegisterStartupScript(this.GetType(), "dire", "alert('恭喜你注册成功，请等待管理员审核');location='login.aspx';", true);
            }
            catch
            {
                ClientScript.RegisterStartupScript(this.GetType(), "err", "alert('注册过程出现异常，注册失败，请稍后再试');", true);
            }
        }
    }
}